package com.ctwom.algorithm.questions.Blinkedlist;

/**
 * 单链表翻转
 * 思路：记住先把原来的头指向空，然后遍历倒一下就可以了
 */
public class Reverse {
    public static void main(String[] args) {
        Node node3 = new Node(3, null);
        Node node2 = new Node(2, node3);
        Node node1 = new Node(1, node2);
        System.out.println(reverse(node1).getValue());
    }

    public static Node reverse(Node head){
        Node pre = null;
        Node next = null;
        while(head != null){
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        Node.forEachNodes(pre);
        System.out.println();
        return pre;
    }
}
